package com.hh.subClass.controller;/*
 * @Auther:ZhengXueCheng
 * @Date:2023/7/24
 * @VERSON:1.8
 */

import com.hh.subClass.vo.AddSubClassVo;
import com.hh.subClass.vo.StatusChangeVo;
import com.hh.subClass.vo.SubClassVo;
import com.hh.subClass.vo.UpdateSubClassVo;
import com.hh.subClass.service.SubClassService;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@RequestMapping("/subClass")
@CrossOrigin
public class SubClassController {
    @Autowired
    private SubClassService subClassService;

    @PostMapping("/queryPage")
    public ResultVo queryPage(@RequestBody SubClassVo subClassVo) {
        return ResultVo.success(subClassService.findAllByPage(subClassVo));
    }

    /**
     * 做备件小类的导入功能
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/importFile")
    public ResultVo importFile(@RequestBody MultipartFile file)throws Exception{
        return subClassService.importFile(file);
    }


    @PostMapping("/addSubClass")
    public ResultVo addSubClass(@RequestBody @Valid AddSubClassVo addSubclassVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return subClassService.addSubClass(addSubclassVo);
    }

    @PostMapping("/updateStatus")
    public ResultVo updateStatus(@RequestBody StatusChangeVo statusChangeVo) {

        if (subClassService.updateStatus(statusChangeVo)) {
            return ResultVo.success();
        } else {
            return ResultVo.error("转化失败");
        }

    }

    @PostMapping("/modifySubClass")
    public ResultVo modifySubClass(@RequestBody UpdateSubClassVo updateSubClassVo) {
        return subClassService.updateSubClass(updateSubClassVo);

    }

    @GetMapping("/subClassAll")
    public ResultVo subClassAll(){
        return subClassService.getAllSubClassList();
    }

}
